package cn.uncode.mq.client.producer;

import java.util.Arrays;
import java.util.List;

import cn.uncode.mq.client.NettyClient;
import cn.uncode.mq.network.Message;
import cn.uncode.mq.network.Topic;
import cn.uncode.mq.network.TransferType;
import cn.uncode.mq.server.RequestHandler;
import cn.uncode.mq.util.DataUtils;

public class Producer extends NettyClient {
	
	
	
	private static final Producer INSTANCE = new Producer();
	
	private Producer(){}
	
	public static void connect(String host, int port){
		INSTANCE.openning(host, port);
	}
	
	public static boolean send(Topic[] topics){
		return send(Arrays.asList(topics));
	}
	
	public static boolean send(List<Topic> topics){
		boolean result = false;
		Message request = Message.newRequestMessage();
		request.setReqHandlerType(RequestHandler.PRODUCER);
		request.setBody(DataUtils.serialize(topics));
		Message response = INSTANCE.write(request);
		if (response.getType() == TransferType.EXCEPTION.value) {
			result = false;
		} else {
			result = true;
		}
		return result;
	}
	
	public static void close(){
		INSTANCE.closing();
	}
	
	
	
	
	

}
